/**
 * 完整的GPS到高德坐标转换工具
 */
function Gaode() {
    this.PI = 3.1415926535897932384626;
    this.a = 6378245.0;
    this.ee = 0.00669342162296594323;
}

Gaode.prototype = {
    // 转换函数（同上，这里省略具体实现）
    transformLat: function(x, y) {
        // 实现同上
        var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
        return ret;
    },
    
    transformLon: function(x, y) {
        // 实现同上
        var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
        return ret;
    },
    
    outOfChina: function(lon, lat) {
        if (lon < 72.004 || lon > 137.8347) return true;
        if (lat < 0.8293 || lat > 55.8271) return true;
        return false;
    },
    
    // GPS转高德
    wgs84ToGcj02: function(lon, lat) {
        if (this.outOfChina(lon, lat)) {
            return { longitude: lon, latitude: lat };
        }
        
        var dLat = this.transformLat(lon - 105.0, lat - 35.0);
        var dLon = this.transformLon(lon - 105.0, lat - 35.0);
        
        var radLat = lat / 180.0 * this.PI;
        var magic = Math.sin(radLat);
        magic = 1 - this.ee * magic * magic;
        var sqrtMagic = Math.sqrt(magic);
        
        dLat = (dLat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtMagic) * this.PI);
        dLon = (dLon * 180.0) / (this.a / sqrtMagic * Math.cos(radLat) * this.PI);
        
        var mgLat = lat + dLat;
        var mgLon = lon + dLon;
        
        return {
            longitude: parseFloat(mgLon.toFixed(6)),
            latitude: parseFloat(mgLat.toFixed(6))
        };
    },
    
    // 高德转GPS
    gcj02ToWgs84: function(lon, lat) {
        if (this.outOfChina(lon, lat)) {
            return { longitude: lon, latitude: lat };
        }
        
        var dLat = this.transformLat(lon - 105.0, lat - 35.0);
        var dLon = this.transformLon(lon - 105.0, lat - 35.0);
        
        var radLat = lat / 180.0 * this.PI;
        var magic = Math.sin(radLat);
        magic = 1 - this.ee * magic * magic;
        var sqrtMagic = Math.sqrt(magic);
        
        dLat = (dLat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtMagic) * this.PI);
        dLon = (dLon * 180.0) / (this.a / sqrtMagic * Math.cos(radLat) * this.PI);
        
        var mgLat = lat + dLat;
        var mgLon = lon + dLon;
        
        return {
            longitude: parseFloat((lon * 2 - mgLon).toFixed(6)),
            latitude: parseFloat((lat * 2 - mgLat).toFixed(6))
        };
    }
};

module.exports = Gaode